<?php
namespace app\admin\controller;
use app\admin\model\Admin as AdminModel;
use app\admin\controller\Base;
use app\admin\controller\Auth;
use think\Db;
use app\admin\controller\Common;
use think\Session;

class Admin extends Base
{
    
    public function lst()
    {
        $auth=new Auth();

        $list=AdminModel::paginate(20);
        foreach ($list as $k => $v) {
            $_groupTitle=$auth->getGroups($v['id']);
            if($_groupTitle){
                $groupTitle=$_groupTitle[0]['title'];
                $v['groupTitle']=$groupTitle;
            }

        }

        $position=db('position')->select();
        $this->assign('position',$position);
        $this->assign('list',$list);
        $authGroupRes=db('auth_group')->select();
        $this->assign('authGroupRes',$authGroupRes);
        return view();
    }

    public function add()
    {
        if (request()->isPost())
        {
            $data=[
                'name'=>input('name'),
                'password'=>md5(input('password')),
                'position_id'=>input('position_id')
            ];
            $validate = \think\Loader::validate('Admin');
            if (!$validate->scene('add')->check($data))
            {
                $this->error($validate->getError());die;
            }
            if (db('admin')->insert($data))
        {
            return $this->success('管理员添加成功','lst');
        }
        else
        {
            return $this->error('管理员添加失败','lst');
        }
            dump(input('post.'));
            return ;
        }
        return $this->fetch();
    }
    
    public function edit()
    {
        $id=input('id');
        $admins=db('admin')->find($id);
        if (request()->isPost())
        {
            $data=[
                'name'=>input('name'),
                'password'=>md5(input('password')),
                'position_id'=>input('position_id')
            ];
            if (input('password'))
            {
                $data['password']=md5(input('password'));
            }
            else
            {
                $data['password']=$admins['password'];
            }
            $validate = \think\Loader::validate('Admin');
            if (!$validate->scene('edit')->check($data))
            {
                $this->error($validate->getError());die;
            }

            $save=db('admin')->where('id','=',$id)->update($data);
            if ($save!==false)
            {
                $this->success('修改管理员成功','lst');
            }
            else {
                $this->error('修改管理员失败');
            }
        }
        
        
        $this->assign('admins',$admins);
        return $this->fetch();
    }
    
    public function del()
    {
        $id=input('id');
        if ($id!=1){
            if (db('admin')->where('id','=',$id)->delete()){
               if(db('auth_group_access')->where('id','=',$id)->delete()){
                   $this->success('管理员删除成功','lst');
               };


            }else {
                $this->error('管理员删除失败','lst');
            }
        }
        else {
            $this->error('不能删除超级管理员','lst');
        }
    }


    public function ajaxEdit(){
        $input = input();
        $res = db('admin')->find($input['eid']);
        $group = db('auth_group_access')->where('uid',$input['eid'])->field('group_id')->find();
        $res['group_id']=$group['group_id'];
        return $this->returnjson(200,$res);
    }

    public function ajaxseave()
    {
        $input=input();
        $id=input('id');
        $admins=db('admin')->find($id);
        if (input('password'))
        {
            $input['password']=md5(input('password'));
        }
        else
        {
            $input['password']=$admins['password'];
        }
        $admins['password']=$input['password'];
        $admins['position_id']=$input['position_id'];
        $save=db('admin')->update($admins);


            $groupAccess['uid']=$input['id'];
            $groupAccess['group_id']=$input['group_id'];
            db('auth_group_access')->where('uid','=',$input['id'])->update($groupAccess);
            return ['ECODE'=>200,'MSG'=>'修改成功'];

    }

    public function ajaxadd()
    {
        $input=input();
        $data=array();
        $data['name']=$input['name'];
        $data['password']=md5($input['password']);
        $data['position_id']=$input['position_id'];
        $save=Db::name('admin')->insertGetId($data);
        if($save){
            $groupAccess['uid']=$save;
            $groupAccess['group_id']=$input['group_id'];
            db('auth_group_access')->insert($groupAccess);
            return ['ECODE'=>200,'MSG'=>'添加成功'];
        }
    }

    public function logout(){
        session(null);
        $this->success('退出成功','login/index');
    }

    //Ajax异步修改管理员状态
    public function changestatus(){
        if(request()->isAjax()){
            $userid=input('userid');
            $status=db('admin')->field('status')->where('id',$userid)->find();
            $status=$status['status'];
            if($status==1){
                db('admin')->where('id',$userid)->update(['status'=>0]);
                echo 1;//由显示改为隐藏
            }
            elseif($status==0){
                db('admin')->where('id',$userid)->update(['status'=>1]);
                echo 2;//由隐藏改为显示
            }
        }
        else{
            $this->error("非法操作");
        }

    }
}
